package com.meta.platform.company.openapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.meta.platform.company.dto.QueryCompanyDto;
import com.meta.platform.company.util.HttpUtil;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class TYCApi {


    private static final String TOKEN = "2aafca2f-7b63-4ef5-9715-63ec73e24b18";

    private static final String LIST_URL = "http://open.api.tianyancha.com/services/open/search/2.0";
    private static final String BASIC_URL = "https://open.api.tianyancha.com/services/open/ic/baseinfoV2/2.0";
    private static final String SHAREHOLDER_URL = "https://open.api.tianyancha.com/services/open/ic/holder/2.0";
    private static final String MEMBER_URL = "https://open.api.tianyancha.com/services/open/ic/staff/2.0";
    private static final String BRANCH_URL = "https://open.api.tianyancha.com/services/open/ic/branch/2.0";
    // TODO：先不加

    private static JSONObject sendRequest(String url) {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", TOKEN);
        String res = HttpUtil.sendGet(url, headers);
        System.out.println(res);

        if (StringUtils.isEmpty(res)){
            return new JSONObject();
        }

        JSONObject result = JSON.parseObject(res);
        if (result.getInteger("error_code") == 0){
            return result.getJSONObject("result");
        }

        return new JSONObject();
    }

    private static String encode(String str){
        try {
            return URLEncoder.encode(str, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return "";
    }


    public static JSONArray queryCompanyList(QueryCompanyDto dto){

        String url = LIST_URL;
        url += ("?word=" + encode(dto.getKeyword()));
        JSONObject res = sendRequest(url);

        if (res == null){
            return new JSONArray();
        }
        return res.getJSONArray("items");

    }

    public static JSONObject queryBasicInfo(QueryCompanyDto dto){
        String url = BASIC_URL;
        url += ("?keyword=" + encode(dto.getKeyword()));
        JSONObject res = sendRequest(url);

        return res;
    }

    public static JSONArray queryShareholderInfo(QueryCompanyDto dto){

        String url = SHAREHOLDER_URL;
        url += ("?keyword=" + encode(dto.getKeyword()));
        return sendRequest(url).getJSONArray("items");
    }

    public static JSONArray queryMemberInfo(QueryCompanyDto dto){
        String url = MEMBER_URL;
        url += ("?keyword=" + encode(dto.getKeyword()));
        return sendRequest(url).getJSONArray("items");
    }

//    public static String queryChangeInfo(QueryCompanyDto dto){
//        String url = CHANGE_URL;
//        url += ("?key=" +  "&");
//        url += dto.toString();
//        String res = sendRequest(url);
//        return res;
//    }

    public static JSONArray queryBranchInfo(QueryCompanyDto dto){
        String url = BRANCH_URL;
        url += ("?keyword=" + encode(dto.getKeyword()));
        return sendRequest(url).getJSONArray("items");
    }

//    public String queryOperationInfo(QueryCompanyDto dto){
//        String url = OPERATION_URL;
//        url += ("?key=" + QCC_KEY + "&");
//        url += dto.toString();
//        String res = sendRequest(url);
//        return res;
//    }

    public static void main(String[] args) {

        QueryCompanyDto dto = new QueryCompanyDto();
        dto.setKeyword("91110000802100433B");
        System.out.println(dto);

        System.out.println(queryBranchInfo(dto));

    }

}
